package com.sunseagear.wind.modules.monitor.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunseagear.common.base.http.R;
import com.sunseagear.common.base.http.RPage;
import com.sunseagear.common.base.mvc.controller.BaseBeanController;
import com.sunseagear.common.utils.DateUtils;
import com.sunseagear.common.utils.ObjectUtils;
import com.sunseagear.common.utils.StringUtils;
import com.sunseagear.wind.common.aspectj.annotation.Log;
import com.sunseagear.wind.common.aspectj.enums.LogType;
import com.sunseagear.wind.common.response.ResponseError;
import com.sunseagear.wind.modules.monitor.entity.LoginLog;
import com.sunseagear.wind.modules.monitor.entity.query.LoginLogQuery;
import com.sunseagear.wind.modules.monitor.service.ILoginLogService;
import com.sunseagear.wind.modules.sys.entity.User;
import com.sunseagear.wind.common.utils.excel.ExportExcel;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 *
 * @version V1.0
 * @package com.sunseagear.wind.modules.sys.controller
 * @title: 登陆日志控制器
 * @description: 登陆日志控制器
 * @author: sys * @date: 2018-09-28 11:35:45
 * @copyright: 2018 www.sunseagear.com Inc. All rights reserved.
 */

@RestController
@RequestMapping("monitor/login/log")
@Log(title = "登陆日志")
@RequiredArgsConstructor
public class LoginLogController extends BaseBeanController {

    private final ILoginLogService loginLogService;


    /**
     * 根据页码和每页记录数，以及查询条件动态加载数据
     */
    @GetMapping(value = "list")
    @Log(logType = LogType.SELECT)
    @SaCheckPermission("monitor:login:log:list")
    public RPage<LoginLog> list(LoginLogQuery query) {
        //加入条件
        QueryWrapper<LoginLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(LoginLog::getLoginTime)
                .eq(ObjectUtils.isNotEmpty(query.getStatus()), LoginLog::getStatus, query.getStatus());
        // 预处理
        Page<LoginLog> pageBean = loginLogService.page(getPage(), queryWrapper);
        return RPage.success(pageBean);
    }

    /**
     * 删除
     * @param idList id列表
     * @return 操作结果
     */
    @DeleteMapping("delete")
    @SaCheckPermission("monitor:login:log:delete")
    public R<Void> delete(@RequestBody List<Long> idList) {
        loginLogService.removeBatchByIds(idList);
        return R.success("删除成功");
    }

    @GetMapping("export")
    @Log(logType = LogType.EXPORT)
    @SaCheckPermission("monitor:login:log:export")
    public R<Map<String, Object>> export() {
        HashMap<String, Object> response = new HashMap<>();
        try {
            //加入条件
            QueryWrapper<LoginLog> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().orderByDesc(LoginLog::getLoginTime);
            Page pageBean = loginLogService.page(getPage(), queryWrapper);
            String title = "登陆日志";
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            new ExportExcel(title , User.class).setDataList(pageBean.getRecords()).write(bos);
            byte[] bytes = bos.toByteArray();
            String bytesRes = StringUtils.bytesToHexString2(bytes);
            title = title + "-" + DateUtils.getDateTime();
            response.put("bytes", bytesRes);
            response.put("title", title);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(ResponseError.NORMAL_ERROR, "导出失败");
        }
        return R.success("导出成功", response);
    }
}
